package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.courseware.exercise.PlannedSchedule;
import co.fitstart.entity.courseware.fitnessgroup.Courseware;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.CourseScheduleService;
import co.fitstart.mobile.service.CoursewareService;
import co.fitstart.mobile.service.WeeklyPlanService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 课件-课件
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/rounds/{courseRoundId}/course-coursewares")
public class CourseCourseWareController {
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private CourseScheduleService courseScheduleService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    @Autowired
    private WeeklyPlanService weeklyPlanService;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping("")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView list(@Secure User user, 
                             @PathVariable("courseRoundId") Long courseRoundId) {
        
        ModelAndView modelAndView = new ModelAndView("course/batch/round-courseware-list");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        modelAndView.addObject("courseRound", courseRound);
        
        if(courseRound != null) {
            List<CourseSchedule> courseSchedules = coursewareService.listCourseSchedule(courseRound);

            modelAndView.addObject("courseSchedules", courseSchedules);
        }
        
        return modelAndView;
    }
    
    
    @RequestMapping(value = "/{courseScheduleId}", method = RequestMethod.GET)
    public ModelAndView courseCourseware(@Secure User user,
                                         @PathVariable("courseRoundId") Long courseRoundId,
                                         @PathVariable("courseScheduleId") Long courseScheduleId) {
        
        ModelAndView modelAndView = new ModelAndView("course/batch/course-courseware");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            CourseSchedule courseSchedule = courseScheduleService.loadCourseSchedule(courseScheduleId);
            
            if(courseSchedule != null) {
                int session = coursewareService.getRank(courseSchedule);
                List<Courseware> coursewares = coursewareService.listCourseWares(courseSchedule);
                PlannedSchedule plannedSchedule = weeklyPlanService.getPlannedSchedule(courseSchedule, session);
                
                modelAndView.addObject("coursewares", coursewares);
                modelAndView.addObject("plannedSchedule", plannedSchedule);
                modelAndView.addObject("courseSchedule", courseSchedule);
                modelAndView.addObject("session", session);
            }

            modelAndView.addObject("courseRound", courseRound);
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param courseRoundId
     * @param coursewareId
     * @return
     */
    @RequestMapping(value = "/{courseScheduleId}/coursewares/{coursewareId}", method = RequestMethod.GET)
    public ModelAndView coureware(@Secure User user,
                                  @PathVariable("courseRoundId") Long courseRoundId,
                                  @PathVariable("courseScheduleId") Long courseScheduleId,
                                  @PathVariable("coursewareId") Long coursewareId) {
        ModelAndView modelAndView = new ModelAndView("course/batch/course-courseware-detail");
        
        Courseware courseware = genericDaoSupport.load(Courseware.class, coursewareId);
        
        if(courseware != null && !courseware.isDisabled() && courseware.getCourseSchedule().getStudent().getUser() == user) {
            modelAndView.addObject("courseware", courseware);
            modelAndView.addObject("session", coursewareService.getRank(courseware.getCourseSchedule()));
        }
        
        return modelAndView;
    }
}
